﻿/*c2.使用事件机制，模拟实现一个闹钟功能。闹钟可以有嘀嗒（Tick）
 * 事件和响铃（Alarm）两个事件。在闹钟嘀嗒运行时或者响铃时，
 * 在控制台显示提示信息。
 */

public delegate void AlarmHander(object sender, AlarmEventArgs args);
public class AlarmEventArgs
{
    public int Hour { get; set; }
    public int Minute { get; set; }

}

public class Alarm
{
    public event AlarmHander SetAlarm;
    public void Set(int h,int m)
    {
        Console.WriteLine($"定时{h}：{m}响铃");
        AlarmEventArgs args = new AlarmEventArgs()
        {
            Hour = h,
            Minute = m
        };
        SetAlarm(this, args);
    }
}

public class Form
{
    public Alarm alarm=new Alarm();

    public Form()
    {
        alarm.SetAlarm += new AlarmHander(Tick);
        alarm.SetAlarm += Ring;
    }
    void Tick(Object sender,AlarmEventArgs args)
    {
        DateTime now = DateTime.Now; 
        DateTime newtime;
        while(now.Hour!=args.Hour||now.Minute!=args.Minute)
        {
            newtime = DateTime.Now;
            if(newtime.Second>now.Second){
                Console.WriteLine("Tick");
                now = newtime;
            }
            if(newtime.Hour == args.Hour && newtime.Minute == args.Minute)
            {
                break;
            }
            
        }

    }
    void Ring(Object sender,AlarmEventArgs args)
    {
        DateTime now = DateTime.Now;
        if (now.Hour == args.Hour && now.Minute == args.Minute)
        {
            Console.WriteLine("Ring");
        }
    }
}
class Program
{
    static void Main(string[] args)
    {
        Form form = new Form();
        form.alarm.Set(16, 36);
    }
}